<template>
  <div class="app-container">
    <el-row :gutter="24" type="flex" justify="between" class="padding">
      <el-col :span="12" class="titles">
        <span class="title">套餐配置</span>
      </el-col>
      <el-col :span="12" class="right">
        <el-button type="primary" size="medium" @click="handleAdd">新增套餐配置</el-button>
      </el-col>
    </el-row>
    <el-table
      v-loading="listLoading"
      :data="list"
      row-key="id"
      border
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column
        type="index"
        width="50">
      </el-table-column>
      <el-table-column
        prop="name"
        label="名称"
      />
      <el-table-column
        prop="amount"
        label="金额"
      />
      <el-table-column
        prop="content"
        label="说明内容"
      />
      <el-table-column
        prop="createTime"
        label="发布时间   "
      />
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="title" width="40%" :visible.sync="dialogForm" @close="colse">
      <el-form ref="form" :model="form" size="mini" :rules="rules">
        <el-form-item label="套餐名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" placeholder="请输入套餐名称" />
        </el-form-item>
        <el-form-item label="套餐金额" :label-width="formLabelWidth" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入套餐金额" />
        </el-form-item>
        <el-form-item label="套餐说明" :label-width="formLabelWidth" prop="content">
          <el-input v-model="form.content" placeholder="请输入套餐说明" />
        </el-form-item>
        <el-form-item label="所属分组" :label-width="formLabelWidth" prop="parentId">
          <el-select v-model="form.parentId" placeholder="请选择所属分组">
            <el-option v-for="item in topDataList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogForm = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getMeal, mealRemove, saveMeal, getTopMeal } from '@/api/admin/meal'
import { arrayToTree } from '@/utils/util'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      dialogForm: false,
      form: {
        name: '',
        amount: '',
        content: '',
        parentId: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入套餐名称', trigger: 'blur' }
        ],
        amount: [
          { required: true, message: '请输入套餐金额', trigger: 'blur' }
        ],
      },
      title: '',
      formLabelWidth: '120px',
      optionProps: {
        value: 'id',
        label: 'name',
        children: 'children',
        checkStrictly: true
      },
      topDataList: []
    }
  },
  created() {
    this.getList()
    this.getTopMealList()
  },
  methods: {
    handleAvatarSuccess(response, file) {
      this.form.imgUrl = response.data
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/gif' ||
        file
          .type === 'image/webp'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传图片JPG或者PNG格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    colse() {
      this.form = {
        authCode: '',
        detailUrl: '',
        maxSelectCount: '',
        registAgreement: '',
        serviceAgreement: ''
      }
    },
    handleAdd() {
      this.dialogForm = true
      this.title = '新增套餐配置'
    },
    handleEdit(index, row) {
      this.dialogForm = true
      this.form = JSON.parse(JSON.stringify(row))
      this.title = '修改套餐配置'
    },
    submit() {
      this.$refs.form.validate((vaild, done) => {
        if (vaild) {
          saveMeal(this.form).then(response => {
            this.getList()
            this.getTopMealList()
            this.dialogForm = false
            this.form = {
              name: '',
              amount: '',
              content: '',
            }
            this.$message({
              showClose: true,
              message: response.message,
              type: 'success'
            })
          })
        }
      })
    },
    handleDelete(index, row) {
      this.$confirm(
        '删除',
        '删除', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          mealRemove(row.id).then(response => {
            this.getList()
            this.$message({
              showClose: true,
              message: response.message,
              type: 'success'
            })
          })
        })
    },
    getList() {
      this.listLoading = true
      getMeal().then(response => {
        this.list = arrayToTree(response.data)
        this.listLoading = false
      })
    },
    getTopMealList() {
      getTopMeal().then(response => {
        this.topDataList = response.data
      })
    }
  }
}
</script>

<style>
/deep/.el-form-item{
  margin-bottom: 0 !important;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style scoped>
.title{
  padding-left: 7px;
  font-size: 16px;
  font-weight: 500;
  line-height: 24px;
}
.right{
  display: flex;
  justify-content: flex-end;
}
.padding{
  margin-bottom: 20px;
}
.titles{
  display: flex;
  align-items: center;
}
.flex{
  padding-top: 20px;
}
</style>
